<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class login extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $session = $this->m_session->get_session();
        if (!$session['session_email']) {
            $data = array('status' => ''
            );
            $this->load->view('login', $data);
        } else {
            $temp = $this->m_session->get_session();
            $email = $temp['session_email'];
            //Ambil record
            $query = $this->db->query("SELECT * FROM user WHERE email='$email'");
            if ($query->num_rows() > 0) {
                foreach ($query->result() as $row) {                    
                    $data['email'] = $email;
                    $data['name'] = $row->nama;
                    $data['date'] = $row->tanggal_lahir;
                    $data['bio'] = $row->bio;
                    redirect("");
                    $this->load->view('profile', $data);
                }
            }
        }
    }

    function process_login() {
        //load model tabel user
        $this->load->model('m_userid', '', true);
        //tangkap data dari inputan
        $email = $_POST['email'];
        $pass = $_POST['password'];

        //Status jika id ato pass salah
        $data = array('status' => 'Wrong Email or Password'
        );

        //Ambil record
        $query = $this->db->query("SELECT * FROM user WHERE email='$email'");

        //Initialize pass n Combine with salt
        $truePass = '';

        //Ambil record
        $query = $this->db->query("SELECT * FROM user WHERE email='$email'");

        //Bandingkan
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                if ($row->password == $pass) {
                    //Set Session
                    $this->m_session->store_session($email);

                    //go to profile
                    $temp = $this->m_session->get_session();
                    $data['email'] = $temp['session_email'];
                    $data['name'] = $row->nama;
                    $data['date'] = $row->tanggal_lahir;
                    $data['bio'] = $row->bio;
                    $this->load->view('profile', $data);
                } else {
                    $this->load->view('login', $data);
                }
            }
        } else {
            $this->load->view('login', $data);
        }
    }

    function register() {
        $data = array('Status' => "",
            'Default' => ""
        );
        $this->load->view('register', $data);
    }

    function actionlistener() {
        $action = $this->input->post("b_login");
        if ($action == "Login") {
            $this->processLogin();
        } else if ($action == "Register") {
            $this->register();
        }
    }

    function logout() {
        $this->m_session->destroy_session();
        $this->index();
    }

}
